# -*- coding: utf-8 -*- 

from config import config
from lib import hashtag
from twython import Twython, TwythonError
from time import sleep

objConfig = config.config()
objHashtag = hashtag.hashtag()

API_KEY, API_KEY_SECRET = objConfig.getApiKeys()
ACCESS_TOKEN, ACCESS_TOKEN_SECRET = objConfig.getAccessTokens()
COUNTRY_CODE, COUNTRY_NAME, COUNTRY_WOEID = objConfig.getCountryData()
GLOBAL_HASHTAG, GLOBAL_HASHTAG_PLUMP, COUNT_TWEETS, RETWEET = objConfig.getTweetsData()

try:
	apiConnection = Twython(API_KEY, API_KEY_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
	#crear logs
	print u"Authentication: OK"

except Exception, e:
	print u"Authentication: Error -> "

try:
	search_results = apiConnection.search(q = '#' + GLOBAL_HASHTAG, count = COUNT_TWEETS)

except TwythonError as e:
	print e

cont = 0
for tweet in search_results['statuses']:
    cont = cont +1
    #print tweet
    #print cont
    print 'Tweet Data'
    print '\tUser : @%s' % (tweet['user']['screen_name'].encode('utf-8'))
    print '\tDate : %s' % (tweet['created_at'])
    print '\tText : %s' % (tweet['text'].encode('utf-8'))
    print '\tURL : %s' % (tweet['entities']['urls'])
    print '\tFav : %s' % (tweet['favorite_count'])
    print '\tHashtags : %s' % (tweet['entities']['hashtags'])
    if tweet['retweeted']:
    	print '\t# RT : %s' % (tweet['retweet_count'])
    print '************************************************************************'
    print ''

    print objHashtag.concatHashtags(tweet['text'].encode('utf-8'))

    print '************************************************************************'
    print ''